#include "../Include/Configuration.h"

Configuration::Configuration(const string &configFilePath)
    : 
{
}

Configuration::~Configuration()
{
}

Configuration *Configuration::getInstance(const string &configFilePath)
{
    if (nullptr == _pconf)
    {
        _pconf = new Configuration(configFilePath);
    }
    return _pconf;
}

std::map<string, string> &Configuration::getConfigMap()
{
    return _configMap;
}

std::set<string> Configuration::getChineseStopWordList()
{
    return _ChineseStopWordList;
}

std::set<string> Configuration::getEnglishStopWordList()
{
    return _EnglishtStopWordList;
}

std::string Configuration::getFormatedWebPagePath()
{
    std::string path = _configMap["Formated_WebPage_Path"];
    //错误日志
    return path;
}

std::string Configuration::getOffsetFilePath()
{
    std::string path = _configMap["Offset_File_Path"];
    //错误日志
    return path;
}

std::string Configuration::getInvertIndexPath()
{
    std::string path = _configMap["InvertIndex_Path"];
    //错误日志
    return path;
}

void Configuration::readConfigFile()
{
//日志信息
    std::ifstream configFile(_filepath);
    if(!configFile.is_open())
    {
        std::cerr<<"Error opening configuration file: " << _filepath << std::endl;
        return ;
    }   

    std::string line;
    while(std::getline(configFile,line))
    {
        if(line.empty()||line[0]=='#')
        {
            continue;
        }

        size_t delimiterPos = line.find('=');
        if(delimiterPos != std::string::npos)
        {
            std::string key = line.substr(0,delimiterPos);
            std::string value = line.substr(delimiterPos+1);
            _configMap[key] = value;
        }
    }
    configFile.close();
}

void Configuration::readEnglishStopWordFile()
{
    std::string stopWordFilePath = _configMap["EnglishStopWord_File_Path"];
    if(stopWordFilePath.empty())
    {
        std::cerr << "Error: stopword_file not specified in configuration"
                  << std::endl;
        return ;
    }

    std::ifstream stopWordFile(stopWordFilePath);
    if(!stopWordFile.is_open())
    {
        std:: cerr << "Error : opening stopword file : "<<stopWordFilePath
                   << std::endl;
        return ;
    }

    std::string word;
    while(stopWordFile>>word)
    {
        _EnglishStopWordList.insert(word);
    }

    stopWordFile.close();
}

void Configuration::readChineseStopWordFile() {
    std::string stopWordFilePath = _configMap["ChineseStopWord_File_Path"];
    if (stopWordFilePath.empty()) {
        std::cerr << "Error: stopword_file not specified in configuration"
                  << std::endl;
        return;
    }

    std::ifstream stopWordFile(stopWordFilePath);
    if (!stopWordFile.is_open()) {
        std::cerr << "Error opening stopword file: " << stopWordFilePath
                  << std::endl;
        return;
    }

    std::string word;
    while (stopWordFile >> word) {
        _chineseStopWordList.insert(word);
    }

    stopWordFile.close();
}

Configuration::AutoRelease Configuration::_ar;
Configuration *Configuration::_pconf = nullptr;
    

